import { BasicDrawer, useDrawerInner } from "@/components/Drawer";
import { BasicForm, FormSchema, useForm } from "@/components/Form";
import * as UserGroupApi from "@/api/bpm/userGroup";
import * as UserApi from "@/api/system/user";
import { message } from "antd";
import { useTranslation } from "react-i18next";
import { useState, useMemo, useEffect } from "react";
let reload: Function;
let id: any;
const index = (props: { register: Fn }) => {
	const [isUpdated, setIsUpdated] = useState(false);
	const [usersList, setUsersList] = useState([]);
	const { t } = useTranslation();
	const en_vi = "bpm.manager.group";
	const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(props.register, data => {
		reload = data.reload;
		id = data.record?.id;
		resetFields();
		setDrawerProps({ confirmLoading: true });
		setIsUpdated(!!data?.isUpdated);
		setFieldsValue({
			...data.record
		});
		setDrawerProps({ confirmLoading: false });
	});
	const formItems: FormSchema[] = [
		{
			label: t(`${en_vi}.name`),
			component: "Input",
			field: "name",
			rules: [
				{
					required: true,
					message: t("felix_ui.please_enter") + "  " + t(`${en_vi}.name`)
				}
			]
		},
		// {
		// 	label: t(`${en_vi}.users`),
		// 	component: "ApiSelect",
		// 	field: "userIds",
		// 	componentProps: {
		// 		mode: "multiple",
		// 		api: UserApi.getListSimpleUsers,
		// 		labelField: "nickname",
		// 		valueField: "id",
		// 		immediate: false
		// 	}
		// },
		{
			label: t(`${en_vi}.users`),
			component: "Select",
			field: "userIds",
			componentProps: {
				mode: "multiple",
				options: usersList
			},
			rules: [
				{
					required: true,
					message: t("felix_ui.please_select") + "  " + t(`${en_vi}.users`)
				}
			]
		},
		{
			label: t(`felix_ui.status`),
			component: "RadioGroup",
			field: "status",
			componentProps: {
				options: [
					{ label: t(`felix_ui.open`), value: 0 },
					{ label: t(`felix_ui.close`), value: 1 }
				]
			},
			rules: [
				{
					required: true,
					message: t("felix_ui.please_select") + "  " + t(`felix_ui.status`)
				}
			]
		},
		{
			label: t(`${en_vi}.description`),
			component: "InputTextArea",
			field: "description"
		}
	];
	const getTitle = useMemo(() => {
		return isUpdated ? t(`system.menu.update`) : t(`system.menu.add`);
	}, [isUpdated]);
	const [registerForm, { validateFields, setFieldsValue, resetFields }] = useForm({
		schemas: formItems,
		labelWidth: 130,
		baseColProps: {
			span: 24
		}
	});
	const handleSubmit = async () => {
		const formValue = await validateFields();
		let res;
		if (isUpdated) {
			formValue.id = id;
			res = await UserGroupApi.updateUserGroup(formValue);
		} else {
			res = await UserGroupApi.createUserGroup(formValue);
		}
		if (!res) {
			message.error(t("viewComponents.patientInformation.additionFailed"));
			return;
		}
		message.success(t("viewComponents.patientInformation.successfullyAdded"));
		closeDrawer();
		reload();
	};
	useEffect(() => {
		const getUserList = () => {
			UserApi.getListSimpleUsers().then((res: any) => {
				let userList: any = [];
				res.map((item: any) => {
					userList.push({
						label: item.nickname,
						value: item.id
					});
				});
				setUsersList(userList);
			});
		};
		getUserList();
	}, []);
	return (
		<BasicDrawer title={getTitle} showFooter={true} {...props} register={registerDrawer} ok={handleSubmit}>
			<BasicForm register={registerForm} labelWrap={true}></BasicForm>
		</BasicDrawer>
	);
};
export default index;
